<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="上级菜单" prop="pid">
				<el-select v-model="form.pid" placeholder="上级菜单" style="width: 240px">
					    <block v-for="item2 in menuList" :key="item2.id">
							<el-option  :label="item2.title" :value="item2.id" v-if="item2.level==0"></el-option>
							<el-option  :label="'﹂'+item2.title" :value="item2.id" v-if="item2.level==1" style="padding-left: 35px"></el-option>
							<el-option  :label="'﹂'+item2.title" :value="item2.id" v-if="item2.level==2" style="padding-left: 55px"></el-option>
							<el-option  :label="'﹂'+item2.title" :value="item2.id" v-if="item2.level==3" style="padding-left: 75px"></el-option>
						</block>
					     
				</el-select>
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-radio-group v-model="form.type">
					<el-radio-button label="menu">菜单</el-radio-button>
					<el-radio-button label="iframe">Iframe</el-radio-button>
					<el-radio-button label="link">外链</el-radio-button>
					<el-radio-button label="button">按钮</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="菜单图标" prop="meta.icon">
				 <sc-icon-select v-model="form.icon" clearable></sc-icon-select>
			</el-form-item>

			<el-form-item label="菜单名称" prop="title">
				<el-input v-model="form.title" clearable></el-input>
			</el-form-item>
			<el-form-item label="菜单别名" prop="name">
				<el-input v-model="form.name" clearable placeholder="例如：setting.menu"></el-input>
			</el-form-item>
			<el-form-item label="路由地址" prop="path">
						<el-input v-model="form.path" clearable placeholder="例如：/goods/index"></el-input>
			</el-form-item>
			<el-form-item label="模板路径" prop="component">
				<el-input v-model="form.component" clearable placeholder="例如：template/setting/menu"></el-input>
			</el-form-item>
			
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="显示" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
 
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
    import scIconSelect from '@/components/scIconSelect'
	export default {
		components: {
			scIconSelect
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				menuList:[],
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					name: "",
					title: "",
					sort: 0,
					status:'1',
					path:'',
					icon:'',
					component:''
				},
				//验证规则
				rules: {
					path: [
						{required: true, message: '请输入路由地址'}
					],
					name: [
						{required: true, message: '请输入别名'}
					],
					title: [
						{required: true, message: '请输入菜单名称'}
					]
				}
			}
		},
		mounted() {
			this.getMenu()
		},
		methods: {
			async getMenu(){
				if(this.menuList.length > 0){
					 return
				}
				var res = await this.$API.system.menu.list.get();
				this.menuList = res.data;
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				if(mode == 'edit'){
					  this.menuList.push({'id':0,'title':'无','level':0})
				}
				console.log(mode,",,,,,,,,,,,,,,,,,,")
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.demo.postmenu.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.updateMenu()
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			async updateMenu(){
				var menu = await this.$API.system.universal.list.get({},'/system/menu')
				if(menu.code == 200){
					this.$TOOL.data.set("MENU", menu.data.menu)
					this.$TOOL.data.set("PERMISSIONS", menu.data.permissions)
					this.$TOOL.data.set("DASHBOARDGRID", menu.data.dashboardGrid)
					setTimeout(function() {
						window.location.reload();
					},700)
				}
			},
			//表单注入数据
			setData(data ){
				this.form.id = data.id
				this.form.name = data.name
				this.form.title = data.title
				this.form.icon = data.icon
				this.form.path = data.path
				this.form.pid = data.pid
				this.form.sort = data.sort
				this.form.status = data.status
				this.form.type = data.type
				this.form.component = data.component
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>